// RadaRaramDlg.cpp : implementation file
//

#include "stdafx.h"
#include "g_prog1.h"
#include "RadaRaramDlg.h"
#include "RadaParam.h"

// CRadaRaramDlg dialog

IMPLEMENT_DYNAMIC(CRadaRaramDlg, CDialog)

CRadaRaramDlg::CRadaRaramDlg(CRadaParam *prm, CWnd* pParent /*=NULL*/)
: CDialog(CRadaRaramDlg::IDD, pParent)
, m_prm(prm)
, m_nNCLASS(0)
, m_nNOR(0)
, m_nNI(0)
, m_nLL(0)
, m_fXL(0)
, m_dIntegStep(0)
{
	if(m_prm == NULL)
		throw CCustomException(_T("CRadaRaramDlg: Empty param"));
	m_nNCLASS = m_prm->m_NCLASS;
	m_nNOR = m_prm->m_NOR;
	m_nNI = m_prm->m_NI;
	m_nLL = m_prm->m_LL;
	m_fXL = m_prm->m_XL;
	m_dIntegStep = m_prm->m_dIntegrationStep;
}

CRadaRaramDlg::~CRadaRaramDlg()
{
}

void CRadaRaramDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_NOR, m_nNOR);
	DDX_Text(pDX, IDC_EDIT_NI, m_nNI);
	DDX_Text(pDX, IDC_EDIT_LL, m_nLL);
	DDX_Text(pDX, IDC_EDIT_XL, m_fXL);
	DDX_Text(pDX, IDC_EDIT_NCLASS, m_nNCLASS);
	DDX_Text(pDX, IDC_EDIT_STEP, m_dIntegStep);
}


BEGIN_MESSAGE_MAP(CRadaRaramDlg, CDialog)
END_MESSAGE_MAP()


// CRadaRaramDlg message handlers

void CRadaRaramDlg::OnOK()
{
	CStdString strError;
	UpdateData(TRUE);
	
	struct
	{
		LPCTSTR name;
		int	*	pnVal;
		float* pfVal;
		double *pdVal;
		double minVal;
		double maxVal;
	} arr [] = {
		"NCLASS" ,	&m_nNCLASS,	NULL,	NULL,			-2.0,	2.0, 
		"NOR" ,		&m_nNOR,	NULL,	NULL,			1.0,	40.0, 
		"NI" ,		&m_nNI,		NULL,	NULL,			1.0,	3.0, 
		"LL" ,		&m_nLL,		NULL,	NULL,			-1.0,	20.0,
		"XL" ,		NULL,		&m_fXL,	NULL,			0.0,	1.0,
		"Step" ,	NULL,		NULL,	&m_dIntegStep,	0.0,	100.0,
	};

	for(int nPos = 0; nPos < sizeof(arr)/sizeof(arr[0]); ++nPos)
	{
		CStdString curError;
		if(arr[nPos].pnVal)
		{
			if( *arr[nPos].pnVal < arr[nPos].minVal || *arr[nPos].pnVal > arr[nPos].maxVal)
				curError.Format(_T("wrong input value '%s' = %ld\n"), arr[nPos].name, *arr[nPos].pnVal);
		}
		else if(arr[nPos].pfVal)
		{
			if( *arr[nPos].pfVal < arr[nPos].minVal || *arr[nPos].pfVal > arr[nPos].maxVal)
				curError.Format(_T("wrong input value '%s' = %f\n"), arr[nPos].name, *arr[nPos].pfVal);
		}
		else if(arr[nPos].pdVal)
		{
			if( *arr[nPos].pdVal < arr[nPos].minVal || *arr[nPos].pdVal > arr[nPos].maxVal)
				curError.Format(_T("wrong input value '%s' = %lf\n"), arr[nPos].name, *arr[nPos].pdVal);
		}
		else
		{
			throw CCustomException("Dont know about %s", arr[nPos].name);
		}
		if(!curError.IsEmpty())
			strError += curError;
	}

	if(!strError.IsEmpty())
		AfxMessageBox(strError);
	else
	{
		m_prm->m_NCLASS = m_nNCLASS;
		m_prm->m_NOR = m_nNOR;
		m_prm->m_NI = m_nNI;
		m_prm->m_LL = m_nLL;
		m_prm->m_XL = m_fXL;
		m_prm->m_dIntegrationStep = m_dIntegStep;
		CDialog::OnOK();
	}
}
